<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="图片名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入图片名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="图片描述" prop="desc">
        <el-input v-model="queryParams.desc" placeholder="请输入图片描述" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->

      <!-- queryParams.name    examineStatus -->
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型"  @change="handleQuery">
          <el-option v-for="dict in dict.type.type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选用户择类型" clearable @change="handleQuery">
          <el-option v-for="dict in dict.type.user_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="医院名称" prop="hospitalName">
        <el-input v-model="queryParams.hospitalName" placeholder="请输入医院名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="审核状态" prop="examineStatus">
        <!-- <el-input v-model="queryParams.examineStatus" placeholder="请输入图片名称" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select  v-model="queryParams.examineStatus" clearable placeholder="请选择审核状态">
              <!-- <el-option v-for="item in originalData.institution" :key="item.id" :label="item.nickName" :value="item"> -->
              <el-option v-for="item in dict.type.examine_status" :key="item.label" :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
      </el-form-item>
      <!-- <dict-tag :options="dict.type.examine_status" :value="scope.row.examineStatus" /> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['busines:source:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['busines:source:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['busines:source:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['busines:source:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sourceList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="数据ID" align="center" prop="id" /> -->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="科普号" align="center" prop="nickName" />
      <el-table-column label="账号类型" align="center" prop="userType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_type" :value="scope.row.userType" />
          </template>
          </el-table-column>
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="医院名称" align="center" prop="institutionName" />
      <el-table-column label="科室" align="center" prop="deptName" />
      <el-table-column label="头像" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.avatar"></el-image>
        </template>
      </el-table-column>

      <el-table-column label="封面" align="center" prop="coverImg">
        <!-- <el-table-column label="图片" align="center" prop="imageUrl"> -->
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.coverImg"></el-image>
        </template>
      </el-table-column>

      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type" :value="scope.row.type" />
        </template>
      </el-table-column>

      <el-table-column label="职称" align="center" prop="doctorRank">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.doctor_rank" :value="scope.row.doctorRank" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="examineStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.examine_status" :value="scope.row.examineStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
          <!-- <el-button
            size="large"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:source:edit']"
          >修改</el-button> -->
          <!-- <el-button size="large" type="text" icon="el-icon-user" @click="handleExam(scope.row)">审核</el-button> -->
          <el-dropdown @command="handleCommand" v-if="scope.row.examineStatus == 0">
            <span class="el-dropdown-link" @click="CurrentAction(scope.row)">
              审核<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="approve">审核通过</el-dropdown-item>
              <el-dropdown-item command="reject">审核不通过</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:source:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改移动端图片资源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入图片名称" />
        </el-form-item>
        <el-form-item label="图片描述" prop="desc">
          <el-input v-model="form.desc" placeholder="请输入图片描述" />
        </el-form-item>
        <!-- <el-form-item label="图片资源全路径" prop="imageUrl">
          <el-input v-model="form.imageUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
        <el-form-item label="图片资源全路径" prop="imageUrl">
          <myImageUpload :imageUrl="form.imageUrl" @backUrl="backUrl"
            :uploadData="{ 'name': form.name, 'desc': form.desc }">
          </myImageUpload>
          <!-- <el-input v-model="form.imageUrl" type="textarea" placeholder="请输入内容" /> -->
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核不通过" :visible.sync="openExam" width="30%" append-to-body>
      <el-input v-model="noPassReason" placeholder="请输入不通过原因" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="Denial">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核" :visible.sync="succExam" width="30%" append-to-body>
      <el-input v-model="passDoctorCode" placeholder="请输入医生码" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="passConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSource, getSource, delSource, addSource, updateSource, checkAuthorAuthInfoList, updateAuthorAuthInfo } from "@/api/system/article";
import myImageUpload from "@/components/myImageUpload";
export default {
  components: { myImageUpload },
  dicts: ['doctor_rank', 'type', 'user_type', 'examine_status'],
  name: "authType",
  data() {
    return {
      // 遮罩层
      noPassReason: '',
      loading: true,
      // 选中数组
      ids: [],
      rowAction: '',   //选中当前行
      passDoctorCode: '',    //个人审核通过
      succExam: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 移动端图片资源表格数据
      sourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openExam: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        imageUrl: null,
        desc: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.type = "1";
    this.getList();
  },
  methods: {
    backUrl(val) {
      this.form.imageUrl = val;
    },
    /** 查询移动端图片资源列表 */
    getList() {
      this.loading = true;
      checkAuthorAuthInfoList(this.queryParams).then(response => {
        this.sourceList = response.rows
        this.total = response.total || 0;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        imageUrl: null,
        desc: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加移动端图片资源";
    },
    handleCommand(command) {
      // console.log('选中的命令是：', command);
      if (command === 'approve') {
        this.approveAction();
      } else if (command === 'reject') {
        this.rejectAction();
      }
    },
    approveAction() {
      // 处理审核通过的逻辑
      if (this.rowAction.type == 0) {  //个人审核逻辑
        this.succExam = true;
        return
      } else {
        let params = {  //除个人以为审核
          'examineStatus': 1,   //写死是1
          'id': this.rowAction.id,
          'type': this.rowAction.type,
        }
        this.putData(params);
      }
      //this.$modal.msgSuccess("审核通过");
      //console.log('审核通过',this.rowAction);
    },
    CurrentAction(obj) {
      this.rowAction = obj;
      //console.log(obj);
    },
    passConfirm() {
      if (!this.passDoctorCode) {
        this.$modal.msgError('请输入医生码');
      } else {
        let params = {
          'examineStatus': 1,   //写死是1
          'id': this.rowAction.id,
          'type': this.rowAction.type,
          'doctorCode': this.passDoctorCode
        }
        // console.log(params,1111);
        //return
        this.putData(params);
        //this.$modal.msgSuccess('审核通过');
      }
    },
    async putData(data) {
      let res = await updateAuthorAuthInfo(data)
      if (res.code == 200) {
        this.$modal.msgSuccess(res.msg);
        this.getList();
        this.succExam = false
      }
      //console.log(res);
    },
    rejectAction() {
      // 处理审核不通过的逻辑
      this.openExam = true;
      //console.log('审核不通过');
    },
    Denial() {
      if (this.noPassReason) {
        let params = {
          'examineStatus': 2,   //写死是1
          'id': this.rowAction.id,
          'type': this.rowAction.type,
          'examineFailReason': this.noPassReason
        }
        this.noPass(params);
      } else {
        this.$modal.msgError('请输入审核不通过原因');
      }
    },
    async noPass(data) {
      let res = await updateAuthorAuthInfo(data)
      if (res.code == 200) {
        this.$modal.msgSuccess(res.msg);
        this.getList();
        this.openExam = false
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSource(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改移动端图片资源";
      });
    },
    handleExam(row) {
      console.log(row, 'rows');

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.$modal.warning('暂未支持修改功能');
            // updateSource(this.form).then(response => {
            //   this.$modal.msgSuccess("修改成功");
            //   this.open = false;
            //   this.getList();
            // });
          } else {
            this.open = false;
            this.getList();
            // addSource(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除移动端图片资源编号为"' + ids + '"的数据项？').then(function () {
        return delSource(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/source/export', {
        ...this.queryParams
      }, `source_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
